#!/bin/bash

# 智面 API 使用示例 - Curl 脚本
# 展示如何使用 curl 命令与 API 交互

set -e

BASE_URL="http://localhost:8000"
USERNAME="demo_user_$(date +%s)"
EMAIL="${USERNAME}@example.com"
PASSWORD="password123"

echo "======================================"
echo "智面 API Curl 使用示例"
echo "======================================"

# 1. 注册用户
echo -e "\n1. 注册用户..."
REGISTER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/register" \
  -H "Content-Type: application/json" \
  -d "{
    \"username\": \"$USERNAME\",
    \"email\": \"$EMAIL\",
    \"password\": \"$PASSWORD\"
  }")

echo "✅ 注册响应:"
echo "$REGISTER_RESPONSE" | jq '.'

# 2. 登录获取 Token
echo -e "\n2. 用户登录..."
TOKEN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "username=$USERNAME&password=$PASSWORD")

TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.access_token')
echo "✅ 获得 Token: ${TOKEN:0:50}..."

# 3. 获取当前用户信息
echo -e "\n3. 获取当前用户信息..."
curl -s -X GET "$BASE_URL/api/auth/me" \
  -H "Authorization: Bearer $TOKEN" | jq '.'

# 4. 获取岗位列表
echo -e "\n4. 获取岗位列表..."
POSITIONS=$(curl -s -X GET "$BASE_URL/api/positions/" \
  -H "Authorization: Bearer $TOKEN")

echo "$POSITIONS" | jq '.'

# 获取第一个岗位的 ID
POSITION_ID=$(echo "$POSITIONS" | jq -r '.[0].position_id')

if [ "$POSITION_ID" == "null" ] || [ -z "$POSITION_ID" ]; then
  echo "❌ 没有找到岗位，请先运行: python scripts/create_sample_data.py"
  exit 1
fi

echo "✅ 选择岗位 ID: $POSITION_ID"

# 5. 开始面试
echo -e "\n5. 开始面试..."
SESSION_RESPONSE=$(curl -s -X POST "$BASE_URL/api/interview/start" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"position_id\": \"$POSITION_ID\",
    \"cv_text\": \"我是一名有3年经验的Python后端工程师，熟悉FastAPI、Django等框架。\"
  }")

SESSION_ID=$(echo "$SESSION_RESPONSE" | jq -r '.session_id')
echo "✅ 创建会话: $SESSION_ID"

# 6. 第一轮对话
echo -e "\n6. 第一轮对话..."
CHAT_1=$(curl -s -X POST "$BASE_URL/api/interview/chat" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"session_id\": \"$SESSION_ID\",
    \"message\": \"您好，我想应聘这个岗位。我有3年的Python开发经验。\"
  }")

echo "候选人: 您好，我想应聘这个岗位。我有3年的Python开发经验。"
echo "面试官:" 
echo "$CHAT_1" | jq -r '.ai_response'

# 7. 第二轮对话
echo -e "\n7. 第二轮对话..."
CHAT_2=$(curl -s -X POST "$BASE_URL/api/interview/chat" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"session_id\": \"$SESSION_ID\",
    \"message\": \"我主要负责使用FastAPI开发RESTful API，处理高并发请求。\"
  }")

echo "候选人: 我主要负责使用FastAPI开发RESTful API，处理高并发请求。"
echo "面试官:"
echo "$CHAT_2" | jq -r '.ai_response'

# 8. 第三轮对话
echo -e "\n8. 第三轮对话..."
CHAT_3=$(curl -s -X POST "$BASE_URL/api/interview/chat" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"session_id\": \"$SESSION_ID\",
    \"message\": \"我使用过PostgreSQL和Redis，熟悉数据库优化和缓存策略。\"
  }")

echo "候选人: 我使用过PostgreSQL和Redis，熟悉数据库优化和缓存策略。"
echo "面试官:"
echo "$CHAT_3" | jq -r '.ai_response'

# 9. 获取会话消息
echo -e "\n9. 获取会话消息..."
curl -s -X GET "$BASE_URL/api/interview/sessions/$SESSION_ID/messages" \
  -H "Authorization: Bearer $TOKEN" | jq '.'

# 10. 生成分析报告
echo -e "\n10. 生成分析报告..."
ANALYSIS=$(curl -s -X POST "$BASE_URL/api/interview/analyze" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"session_id\": \"$SESSION_ID\"
  }")

echo "======================================"
echo "📊 面试分析报告"
echo "======================================"
echo "$ANALYSIS" | jq -r '.analysis_report'

echo -e "\n======================================"
echo "✅ 示例完成！"
echo "======================================"

